<template>
  <div v-loading="loading" class="dataForm">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="120px"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('申请标题')" prop="actTitle">
            <el-input v-model="form.actTitle" :placeholder="$t('申请标题')" show-word-limit :maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('申请人')" prop="applyUserId">
            <UserSelect
              v-model="form.applyUserId"
              :placeholder="$t('申请人')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('申请时间')" prop="applyTime">
            <DatePicker
              v-model="form.applyTime"
              :placeholder="$t('申请时间')"
            />

          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('申请部门')" prop="applyDeptId">
            <DeptSelect v-model="form.applyDeptId" :placeholder="$t('申请部门')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('使用部门')" prop="useDeptId">
            <DeptSelect v-model="form.useDeptId" :placeholder="$t('使用部门')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('合同编号')" prop="conteractNum">
            <el-input v-model="form.conteractNum" :placeholder="$t('合同编号')" show-word-limit :maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('合同名称')" prop="name">
            <el-input v-model="form.name" :placeholder="$t('合同名称')" show-word-limit :maxlength="50" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item :label="$t('合同类型(一级)')" prop="typeOne">
            <el-select v-model="form.typeOne" value="" :placeholder="$t('合同类型(一级)')" filterable clearable @change="typeOneChange(form.typeOne, true)">
              <el-option
                v-for="dict in typeOneOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('合同类型(二级)')" prop="typeTwo">
            <el-select v-model="form.typeTwo" value="" :placeholder="$t('合同类型(二级)')" filterable clearable @change="changeIsPayment(form.typeTwo)">
              <el-option
                v-for="dict in getTypeOneChange"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="form.typeTwo === 'QT'" :span="12">
          <el-form-item :label="$t('其他合同类型')" prop="typeOther">
            <el-input v-model="form.typeOther" :placeholder="$t('其他合同类型')" clearable maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('预计签约时间')" prop="signingTime">
            <DatePicker
              v-model="form.signingTime"
              :placeholder="$t('预计签约时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('对方签约主体名称')" prop="company">
            <el-input v-model="form.company" :placeholder="$t('对方签约主体名称')" show-word-limit :maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('收付款类型')" prop="isPayment">
            <el-radio-group v-model="form.isPayment" size="small" @change="changeIsPay(form.isPayment)">
              <el-radio-button
                v-for="dict in isPaymentOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{ $t(dict.dictLabel) }}</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('是否用印')" prop="isSeal">
            <el-radio-group v-model="form.isSeal" size="small">
              <el-radio-button
                v-for="dict in isSealOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{ $t(dict.dictLabel) }}</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item :label="$t('有效期')">
            <el-input v-model="form.usefulLife" :placeholder="$t('有效期')" show-word-limit :maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col v-if="form.typeTwo === 'CG'" :span="12">
          <el-form-item :label="$t('采购申请')" prop="purchasingApplication">
            <el-select v-model="form.purchasingApplication" :placeholder="$t('采购申请')" filterable clearable>
              <el-option
                v-for="dict in listOaPurchaseApplyOption"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="form.typeTwo === 'CG'" :span="12">
          <el-form-item :label="$t('数量')" prop="specifications">
            <el-input v-model="form.specifications" :placeholder="$t('数量')" show-word-limit :maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col v-if="form.typeOne === 'deal' || form.typeOne === 'manager' || form.typeOne === 'hhqy' || form.typeOne === 'fund' || form.typeOne === 'subFund' || form.typeOne === 'lpInvestor'" :span="12">
          <el-form-item
            :rules="form.typeOne !== 'manager' && form.typeOne !== 'fund' ? rules.projectOrFund:[{ required: false}]"
            prop="projectOrFund"
          >
            <MainBodySelect
              v-model="form.projectOrFund"
              :show-label="true"
              :parameters="form.typeOne"
              :required-logo="form.typeOne !== 'manager' && form.typeOne !== 'fund'"
              width="100%"
              @change="getInvestDate(form.projectOrFund)"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.isPayment === 'collection' || form.isPayment === 'payment'" :label="$t('合同金额') + $t('金额单位')" prop="money">
            <Money v-model="form.money" unit="w" :placeholder="$t('合同金额')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('是否采用公司合同范本')" prop="usingCompanyAndTemplates">
            <el-select v-model="form.usingCompanyAndTemplates" :placeholder="$t('请选择')" filterable clearable>
              <el-option
                v-for="dict in isSealOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="form.usingCompanyAndTemplates === 'Y'" :span="12">
          <el-form-item :label="$t('是否对范本进行修改')" prop="modifyTheTemplate">
            <el-select v-model="form.modifyTheTemplate" :placeholder="$t('请选择')" clearable>
              <el-option
                v-for="dict in isSealOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="form.usingCompanyAndTemplates === 'Y' && form.modifyTheTemplate === 'Y'" :span="24">
          <el-form-item :label="$t('请简要说明修改的主要内容及修改原因')" prop="reasonForModification">
            <el-input v-model="form.reasonForModification" type="textarea" :placeholder="$t('若未修改或未采用合同范本, 请填写\'不适用\'')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('合同摘要')" prop="abstracts">
            <el-input v-model="form.abstracts" type="textarea" :placeholder="$t('合同摘要')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
        <el-col v-if="form.isPayment === 'payment'" :span="24">
          <el-form-item :label="$t('付款先决条件')" prop="paymentPrerequisites">
            <el-input v-model="form.paymentPrerequisites" type="textarea" maxlength="2000" show-word-limit :placeholder="$t('付款先决条件')" />
          </el-form-item>
        </el-col>
      </el-row>

      <!--用印明细-->
      <div v-if="form.isSeal === 'Y'" style="margin-top:8px;">
        <div class="area-header">
          <span class="area-header-title">{{ $t('用印明细') }}</span>
        </div>
        <el-table :data="form.oaSealDetailList" border class="nowrapTable formTable">
          <el-table-column :label="$t('序号')" align="center" width="50">
            <template slot-scope="scope">
              {{ scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column :label="$t('用印主体类型')" align="center">
            <template slot="header" slot-scope="{}">
              <i style="color: #ff4949;margin-right: 4px;">*</i>
              <span>{{ $t('用印主体类型') }}</span>
            </template>
            <template slot-scope="scope">
              <el-form-item :prop="'oaSealDetailList.' + scope.$index + '.investMainType'" :rules="oaSealDetailListRules.investMainType">
                <dictSelect
                  v-model="scope.row.investMainType"
                  :clearable="true"
                  dictionary="main_body_type_seal"
                />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column :label="$t('用印主体')" align="center">
            <template slot="header" slot-scope="{}">
              <i style="color: #ff4949;margin-right: 4px;">*</i>
              <span>{{ $t('用印主体') }}</span>
            </template>
            <template slot-scope="scope">
              <el-form-item :prop="'oaSealDetailList.' + scope.$index + '.investMain'" :rules="oaSealDetailListRules.investMain">
                <MainBodySelect
                  v-model="scope.row.investMain"
                  :parameters="scope.row.investMainType"
                />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column :label="$t('用印类型')">
            <template slot="header" slot-scope="{}">
              <i style="color: #ff4949;margin-right: 4px;">*</i>
              <span>{{ $t('用印类型') }}</span>
            </template>
            <template slot-scope="scope">
              <el-form-item :prop="'oaSealDetailList.' + scope.$index + '.sealType'" :rules="oaSealDetailListRules.sealType">
                <el-select
                  v-model="scope.row.sealType"
                  multiple
                  :placeholder="$t('请选择')"
                >
                  <el-option
                    v-for="dict in sealTypeDict"
                    :key="dict.dictValue"
                    :label="$t(dict.dictLabel)"
                    :value="dict.dictValue"
                  />
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column :label="$t('备注')" align="center">
            <template slot-scope="scope">
              <el-form-item :prop="'oaSealDetailList.' + scope.$index + '.remark'" :rules="oaSealDetailListRules.remark">
                <el-input v-model="scope.row.remark" type="textarea" maxlength="200" class="fileTextarea" />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column v-if="!otherBtnHide" :label="$t('操作')" align="center" width="60px">
            <template slot-scope="scope">
              <el-button v-hasPermi="['oa:contract:remove']" size="small" type="text" icon="el-icon-delete" @click="delHandle(scope.$index, scope.row)" />
            </template>
          </el-table-column>
        </el-table>
        <div v-if="!otherBtnHide" class="tol">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="addPayment">{{ $t("添加") }}</el-button>
        </div>
      </div>

      <!-- 签约 主体 -->
      <div v-loading="contractInvestLoading">
        <div style="margin-top:15px;">
          <div class="area-header">
            <span class="area-header-title">{{ $t('签约主体') }}</span>
          </div>
          <el-table v-loading="oaContractPaymentLoading" :data="form.oaContractPaymentList" border class="nowrapTable formTable">
            <el-table-column :label="$t('序号')" align="center" width="50">
              <template slot-scope="scope">
                {{ scope.$index + 1 }}
              </template>
            </el-table-column>
            <el-table-column :label="$t('主体类型')" align="center">
              <template slot="header" slot-scope="{}">
                <i style="color: #ff4949;margin-right: 4px;">*</i>
                <span>{{ $t('主体类型') }}</span>
              </template>
              <template slot-scope="scope">
                <el-form-item :prop="'oaContractPaymentList.' + scope.$index + '.investType'" :rules="oaContractPaymentListRules.investType">
                  <dictSelect
                    v-model="scope.row.investType"
                    :clearable="true"
                    dictionary="main_body_type"
                    @change="oneInvestType = scope.row.investType; scope.row.investMain = undefined; $forceUpdate()"
                  />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column :label="$t('主体名称')" align="center">
              <template slot="header" slot-scope="{}">
                <i style="color: #ff4949;margin-right: 4px;">*</i>
                <span>{{ $t('主体名称') }}</span>
              </template>
              <template slot-scope="scope">
                <el-form-item :prop="'oaContractPaymentList.' + scope.$index + '.investMain'" :rules="oaContractPaymentListRules.investMain">
                  <MainBodySelect
                    v-model="scope.row.investMain"
                    refs="mainBodyInvest"
                    :parameters="oneInvestType"
                    @change="changeInvestData"
                  />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column v-if="isShowStake" :label="$t('持股比例(%)')" align="center">
              <template slot="header" slot-scope="{}">
                <i style="color: #ff4949;margin-right: 4px;">*</i>
                <span>{{ $t('持股比例(%)') }}</span>
              </template>
              <template slot-scope="scope">
                <el-form-item :prop="'oaContractPaymentList.' + scope.$index + '.stake'" :rules="oaContractPaymentListRules.stake">
                  <el-input-number v-model="scope.row.stake" :controls="false" :precision="0" :max="100" />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column :label="$t('备注')" align="center">
              <template slot="header" slot-scope="{}">
                <span>{{ $t('备注') }}</span>
              </template>
              <template slot-scope="scope">
                <el-form-item :prop="'oaContractPaymentList.' + scope.$index + '.remark'">
                  <el-input v-model="scope.row.remark" type="textarea" maxlength="2000" class="fileTextarea" />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column v-if="!otherBtnHide" :label="$t('操作')" align="center" width="60px">
              <template slot-scope="scope">
                <el-button v-hasPermi="['oa:contract:remove']" size="small" type="text" icon="el-icon-delete" @click="delHandlePay(scope.$index, scope.row)" />
              </template>
            </el-table-column>
          </el-table>
          <div v-if="!otherBtnHide" class="tol">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="addPaymentPay">{{ $t("添加") }}</el-button>
          </div>
        </div>

        <!-- ********************************** 收付款计划 ************************************ -->
        <div v-if="nameReceiptPayment === '付款' && (form.isPayment === 'payment' || form.isPayment === 'collection')" v-loading="contractInvestLoading" style="margin-top:15px;">
          <div class="area-header">
            <span class="area-header-title">{{ $t(nameReceiptPayment + '计划') }}</span>
          </div>
          <el-table v-loading="oaContractPaymentLoading" :data="form.oaContractPlanList" border class="nowrapTable formTable">
            <el-table-column :label="$t('序号')" align="center" width="50">
              <template slot-scope="scope">
                {{ scope.$index + 1 }}
              </template>
            </el-table-column>
            <el-table-column :label="$t('主体名称')" align="center">
              <template slot="header" slot-scope="{}">
                <i style="color: #ff4949;margin-right: 4px;">*</i>
                <span>{{ $t('主体名称') }}</span>
              </template>
              <template slot-scope="scope">
                <el-form-item :prop="'oaContractPlanList.' + scope.$index + '.investId'" :rules="oaContractPlanListRules.investId">
                  <el-select v-model="scope.row.investId" clearable size="small">
                    <el-option
                      v-for="(dict, index) in oaContractPlanInvestMainOptions"
                      :key="index"
                      :label="$t(dict.dictLabel)"
                      :value="dict.dictValue"
                    />
                  </el-select>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column :label="$t('预计付款时间')" align="center">
              <template slot="header" slot-scope="{}">
                <i style="color: #ff4949;margin-right: 4px;">*</i>
                <span>{{ $t('预计付款时间') }}</span>
              </template>
              <template slot-scope="scope">
                <el-form-item :prop="'oaContractPlanList.' + scope.$index + '.paymentTime'" :rules="oaContractPlanListRules.paymentTime">
                  <DatePicker
                    v-model="scope.row.paymentTime"
                    :placeholder="$t('预计付款时间')"
                  />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column :label="$t('预计付款金额') + $t('金额单位')" align="center">
              <template slot="header" slot-scope="{}">
                <i style="color: #ff4949;margin-right: 4px;">*</i>
                <span>{{ $t('预计付款金额') }}{{ $t('金额单位') }}</span>
              </template>
              <template slot-scope="scope">
                <el-form-item :prop="'oaContractPlanList.' + scope.$index + '.paymentMoney'" :rules="oaContractPlanListRules.paymentMoney">
                  <Money v-model="scope.row.paymentMoney" unit="w" @change="sumMoneyPayColl('payment')" />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column :label="$t('币种')" align="center">
              <template slot="header" slot-scope="{}">
                <i style="color: #ff4949;margin-right: 4px;">*</i>
                <span>{{ $t('币种') }}</span>
              </template>
              <template slot-scope="scope">
                <el-form-item :prop="'oaContractPlanList.' + scope.$index + '.currency'" :rules="oaContractPlanListRules.currency">
                  <dictSelect
                    v-model="scope.row.currency"
                    :clearable="true"
                    dictionary="currency"
                  />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column :label="$t('备注')" align="center">
              <template slot="header" slot-scope="{}">
                <span>{{ $t('备注') }}</span>
              </template>
              <template slot-scope="scope">
                <el-form-item :prop="'oaContractPlanList.' + scope.$index + '.remark'">
                  <el-input v-model="scope.row.remark" type="textarea" maxlength="2000" class="fileTextarea" />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column v-if="!otherBtnHide" :label="$t('操作')" align="center" width="60px">
              <template slot-scope="scope">
                <el-button v-hasPermi="['oa:contract:remove']" size="small" type="text" icon="el-icon-delete" @click="delHandlePlan(scope.$index, scope.row, 'payment')" />
              </template>
            </el-table-column>
          </el-table>
          <div v-if="!otherBtnHide" class="tol">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="addPaymentPlan">{{ $t("添加") }}</el-button>
          </div>
        </div>

        <div v-if="nameReceiptPayment === '收款' && (form.isPayment === 'payment' || form.isPayment === 'collection')" v-loading="contractInvestLoading" style="margin-top:15px;">
          <div class="area-header">
            <span class="area-header-title">{{ $t(nameReceiptPayment + '计划') }}</span>
          </div>
          <el-table v-loading="oaContractPaymentLoading" :data="form.oaContractPlanList" border class="nowrapTable formTable">
            <el-table-column :label="$t('序号')" align="center" width="50">
              <template slot-scope="scope">
                {{ scope.$index + 1 }}
              </template>
            </el-table-column>
            <el-table-column :label="$t('主体名称')" align="center">
              <template slot="header" slot-scope="{}">
                <i style="color: #ff4949;margin-right: 4px;">*</i>
                <span>{{ $t('主体名称') }}</span>
              </template>
              <template slot-scope="scope">
                <el-form-item :prop="'oaContractPlanList.' + scope.$index + '.investId'" :rules="oaContractPlanListRules.investId">
                  <el-select v-model="scope.row.investId" clearable size="small">
                    <el-option
                      v-for="(dict, index) in oaContractPlanInvestMainOptions"
                      :key="index"
                      :label="$t(dict.dictLabel)"
                      :value="dict.dictValue"
                    />
                  </el-select>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column :label="$t('预计收款时间')" align="center">
              <template slot="header" slot-scope="{}">
                <i style="color: #ff4949;margin-right: 4px;">*</i>
                <span>{{ $t('预计收款时间') }}</span>
              </template>
              <template slot-scope="scope">
                <el-form-item :prop="'oaContractPlanList.' + scope.$index + '.collectionTime'" :rules="oaContractPlanListRules.collectionTime">
                  <DatePicker
                    v-model="scope.row.collectionTime"
                    :placeholder="$t('预计收款时间')"
                  />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column :label="$t('预计收款金额') + $t('金额单位')" align="center">
              <template slot="header" slot-scope="{}">
                <i style="color: #ff4949;margin-right: 4px;">*</i>
                <span>{{ $t('预计收款金额') }}{{ $t('金额单位') }}</span>
              </template>
              <template slot-scope="scope">
                <el-form-item :prop="'oaContractPlanList.' + scope.$index + '.collectionMoney'" :rules="oaContractPlanListRules.collectionMoney">
                  <Money v-model="scope.row.collectionMoney" unit="w" @change="sumMoneyPayColl('collection')" />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column :label="$t('币种')" align="center">
              <template slot="header" slot-scope="{}">
                <i style="color: #ff4949;margin-right: 4px;">*</i>
                <span>{{ $t('币种') }}</span>
              </template>
              <template slot-scope="scope">
                <el-form-item :prop="'oaContractPlanList.' + scope.$index + '.currency'" :rules="oaContractPlanListRules.currency">
                  <dictSelect
                    v-model="scope.row.currency"
                    :clearable="true"
                    dictionary="currency"
                  />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column :label="$t('款项名称')" align="center">
              <template slot="header" slot-scope="{}">
                <i style="color: #ff4949;margin-right: 4px;">*</i>
                <span>{{ $t('款项名称') }}</span>
              </template>
              <template slot-scope="scope">
                <el-form-item :prop="'oaContractPlanList.' + scope.$index + '.remark'" :rules="oaContractPlanListRules.remark">
                  <el-input v-model="scope.row.remark" :placeholder="$t('款项名称')" maxlength="200" />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column v-if="!otherBtnHide" :label="$t('操作')" align="center" width="60px">
              <template slot-scope="scope">
                <el-button v-hasPermi="['oa:contract:remove']" size="small" type="text" icon="el-icon-delete" @click="delHandlePlan(scope.$index, scope.row, 'collection')" />
              </template>
            </el-table-column>
          </el-table>
          <div v-if="!otherBtnHide" class="tol">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="addPaymentPlan">{{ $t("添加") }}</el-button>
          </div>
        </div>
      </div>

      <!--合同关键条款-->
      <div style="margin-top:15px;">
        <div class="area-header">
          <span class="area-header-title">{{ $t('合同关键条款') }}</span>
        </div>
        <el-table :data="form.oaContractTermsList" border class="nowrapTable formTable">
          <el-table-column :label="$t('序号')" align="center" width="50">
            <template slot-scope="scope">
              {{ scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column :label="$t('条款类型')" align="center" min-width="200">
            <template slot="header" slot-scope="{}">
              <i style="color: #ff4949;margin-right: 4px;">*</i>
              <span>{{ $t('条款类型') }}</span>
            </template>
            <template slot-scope="scope">
              <el-form-item :prop="'oaContractTermsList.' + scope.$index + '.type'" :rules="oaContractTermsListRules.type">
                <el-select v-model="scope.row.type" :placeholder="$t('条款类型')">
                  <el-option
                    v-for="dict in typeOptions"
                    :key="dict.dictValue"
                    :label="$t(dict.dictLabel)"
                    :value="dict.dictValue"
                  />
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column :label="$t('条款内容')" align="center" min-width="200">
            <template slot="header" slot-scope="{}">
              <i style="color: #ff4949;margin-right: 4px;">*</i>
              <span>{{ $t('条款内容') }}</span>
            </template>
            <template slot-scope="scope">
              <el-form-item :prop="'oaContractTermsList.' + scope.$index + '.remark'" :rules="oaContractTermsListRules.remark">
                <el-input
                  v-model="scope.row.remark"
                  type="textarea"
                  :autosize="{ minRows: 1, maxRows: 8}"
                  maxlength="2000"
                />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column :label="$t('定时提醒')" align="center" min-width="100">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="remindHandle(scope.$index)">{{ $t('定时任务') }}</el-button>
            </template>
          </el-table-column>
          <el-table-column v-if="!otherBtnHide" :label="$t('操作')" align="center" width="60px">
            <template slot-scope="scope">
              <el-button v-hasPermi="['oa:contract:remove']" size="small" type="text" icon="el-icon-delete" @click="delHandleTerms(scope.$index, scope.row)" />
            </template>
          </el-table-column>
        </el-table>
        <div v-if="!otherBtnHide" class="tol">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="addPaymentTerms">{{ $t("添加") }}</el-button>
        </div>
      </div>

      <!--文件-->
      <FileTable :list.sync="form.fileList" :data-type="dataType" :business-id="(['fund', 'deal', 'manager'].includes(form.typeOne)) ? form.projectOrFund : undefined" :btn-show="!otherBtnHide" />

    </el-form>

    <!-- 定时提醒 -->
    <el-dialog v-if="remindOpen" :title="$t('定时提醒')" :visible.sync="remindOpen" width="40%" :destroy-on-close="true" :close-on-click-modal="false" :lock-scroll="false" append-to-body>
      <el-form
        v-if="form.oaContractTermsList[termsIndex]"
        ref="remindForm"
        :model="form.oaContractTermsList[termsIndex]"
        :rules="remindRules"
        label-width="120px"
        label-position="top"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('提醒频率')" prop="reminderFrequency">
              <el-radio-group v-model="form.oaContractTermsList[termsIndex].reminderFrequency">
                <el-radio
                  v-for="dict in reminderFrequencyOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{ $t(dict.dictLabel) }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col v-if="form.oaContractTermsList[termsIndex].reminderFrequency === 'TIMER'" :span="12">
            <el-form-item :label="$t('定时提醒时间')" prop="remindTime">
              <el-date-picker
                v-model="form.oaContractTermsList[termsIndex].remindTime"
                clearable
                style="width: 200px"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                format="yyyy-MM-dd HH:mm"
                :placeholder="$t('定时提醒时间')"
              />
            </el-form-item>
          </el-col>
          <el-col v-else :span="24">
            <el-form-item :label="$t('频率提醒表达式')" prop="cron">
              <Cron v-model="form.oaContractTermsList[termsIndex].cron" :cron-value.sync="form.oaContractTermsList[termsIndex].frequencyCronValue" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('提醒状态')" prop="status">
              <el-switch
                v-model="form.oaContractTermsList[termsIndex].status"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-text="$t('开启')"
                :inactive-text="$t('关闭')"
                :active-value="1"
                :inactive-value="0"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('提醒人')" prop="remindUserId">
              <UserSelect
                v-model="form.oaContractTermsList[termsIndex].remindUserId"
                :multiple="true"
                :placeholder="$t('提醒人')"
                :popper-append-to-body="true"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="remindConfirmHandle">{{ $t('确定') }}</el-button>
      </div>
    </el-dialog>

    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <FormBtn ref="formBtn" :bus-data="form" has-permi="oa:contract" :btn-hide="btnHide" :menu-key="menuKey" />
    </div>
  </div>
</template>

<script>
  import { saveOaContract, getOaContract, delOaContractTerms, delOaContractPayment, delOaContractPlan, getLpViewByMainBodyId } from '@/api/oa/contract/oaContract'
  import { listDealInvestment, findByDealId } from '@/api/deal/investment/dealInvestment'
  import { deleteOaSealDetail } from '@/api/oa/seal/oaSeal'
  import MainBodySelect from '@/components/MainBodySelect'
  import { deleteFileBase } from '@/api/file/filefolder/folder.js'
  import { getLpView } from '@/api/base/deal/dealBase'
  import { getUser } from '@/api/system/user'
  import { listOaPurchaseApplySelect } from '@/api/oa/purchaseApply/oaPurchaseApply.js'
  import Cron from '@/components/Cron'
  import { ruoyiFloat } from '@/utils/ruoyi'
  export default {
    components: {
      MainBodySelect,
      Cron
    },
    data() {
      return {
        // 菜单标识
        menuKey: 'oa_contract',
        // 是否隐藏保存按钮
        btnHide: false,
        // 是否隐藏其它按钮
        otherBtnHide: false,
        loading: false,
        oaContractPaymentLoading: false,
        // 单选
        isSealOptions: [],
        isPaymentOptions: [],
        // 是否展示持股比例
        isShowStake: false,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          projectOrFund: [{ required: true, message: this.$t('必填信息'),	trigger: ['change', 'blur'] }],
          applyUserId: [{ required: true, message: this.$t('必填信息'),	trigger: ['change', 'blur'] }],
          applyTime: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
          name: [{ required: true, message: this.$t('必填信息'),	 trigger: 'blur' }],
          money: [{ required: true, message: this.$t('必填信息'),	trigger: ['change', 'blur'] }],
          typeOne: [{ required: true, message: this.$t('必填信息'),	trigger: ['change', 'blur'] }],
          typeTwo: [{ required: true, message: this.$t('必填信息'),	trigger: ['change', 'blur'] }],
          // isPayment: [{ required: true, message: this.$t('必填信息'),	trigger: ['change', 'blur'] }],
          // signingTime: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
          actTitle: [{ required: true, message: this.$t('必填信息'), 	trigger: 'blur' }],
          applyDeptId: [{ required: true, message: this.$t('必填信息'), 	trigger: 'blur' }],
          useDeptId: [{ required: true, message: this.$t('必填信息'), 	trigger: 'blur' }],
          company: [{ required: true, message: this.$t('必填信息'), 	trigger: 'blur' }],
          modifyTheTemplate: [{ required: true, message: this.$t('必填信息'), 	trigger: 'blur' }],
          reasonForModification: [{ required: true, message: this.$t('必填信息'), 	trigger: 'blur' }],
          usingCompanyAndTemplates: [{ required: true, message: this.$t('必填信息'), 	trigger: 'blur' }]
        },
        // 操作按钮默认不展示
        buttonShow: true,
        oaSealDetailListRules: {
          investMainType: [{ required: true, message: this.$t('必填信息'), 	trigger: 'blur' }],
          investMain: [{ required: true, message: this.$t('必填信息'), 	trigger: 'blur' }],
          sealType: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
        },
        oaContractPaymentListRules: {
          investType: [{ required: true, message: this.$t('必填信息'),	trigger: ['change', 'blur'] }],
          investMain: [{ required: true, message: this.$t('必填信息'),	trigger: ['change', 'blur'] }],
          stake: [{ required: true, message: this.$t('必填信息'),	trigger: ['blur'] }]
        },
        oaContractPlanListRules: {
          investId: [{ required: true, message: this.$t('必填信息'),	trigger: ['change', 'blur'] }],
          paymentTime: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
          paymentMoney: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
          collectionTime: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
          collectionMoney: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
          remark: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
        },
        oaContractTermsListRules: {
          type: [{ required: true, message: this.$t('必填信息'),	trigger: ['change', 'blur'] }],
          remark: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
        },
        getTypeOneChange: [],
        uploaderOpen: false,
        dataType: 'oa_contract',
        title: '',
        sealTypeDict: undefined,
        // 收款  付款 名称
        nameReceiptPayment: '',
        oaContractPlanInvestMainOptions: [],
        oaContractPlanInvestMainOptionsTemp: [],
        contractInvestLoading: false,
        typeOptions: [],
        typeOneBeforeChange: '',
        listOaPurchaseApplyOption: [],
        requiredLogo: true,
        typeOneOptions: [],
        // 定时提醒
        remindOpen: false,
        // 点击定时提醒关键条款索引
        termsIndex: undefined,
        // 提醒频率字典
        reminderFrequencyOptions: [],
        oneInvestType: undefined,
        remindRules: {
          timerReminderTime: { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] },
          cron: { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] },
          reminderFrequency: { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] },
          remindTime: { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] },
          remindUserId: { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
        }
      }
    },
    watch: {
      'form.isPayment': {
        handler(val) {
          if (val === 'collection') {
            this.nameReceiptPayment = '收款'
          } else if (val === 'payment') {
            this.nameReceiptPayment = '付款'
          } else {
            this.nameReceiptPayment = '收付款'
          }
          if (this.form.typeTwo === 'TZXY' && val === 'payment') {
            this.isShowStake = true
          } else {
            this.isShowStake = false
          }
        }
      },
      'form.typeTwo': {
        handler(val) {
          if (val === 'TZXY' && this.form.isPayment === 'payment') {
            this.isShowStake = true
          } else {
            this.isShowStake = false
          }
        }
      },
      'form.money': {
        handler(val) {
          if (isNaN(val)) {
            this.form.money = 0
          }
        }
      }
    },
    created() {
      this.$bus.$emit('async_done')
      this.getDicts('sys_yes_no').then(response => {
        this.isSealOptions = response.data
      })
      this.getDicts('collection_payment_no').then(response => {
        this.isPaymentOptions = response.data
      })
      this.getDicts('seal_type').then(response => {
        this.sealTypeDict = response.data
      })
      this.getDicts('contract_type').then(response => {
        this.typeOneOptions = response.data
      })
      this.getDicts('reminder_frequency').then(response => {
        this.reminderFrequencyOptions = response.data
      })
      // 采购申请
      listOaPurchaseApplySelect({ actStatus: 'W' }).then(response => {
        this.listOaPurchaseApplyOption = []
        if (response.data) {
          response.data.forEach(item => {
            this.listOaPurchaseApplyOption.push({ dictLabel: item.actTitle, dictValue: item.id })
          })
        }
      })
    },
    methods: {
      // 初始化数据
      init(data) {
        this.oaContractPlanInvestMainOptionsTemp = []
        this.oaContractPlanInvestMainOptions = []
        if (!this.isEmpty(data) && !this.isEmpty(data.id)) {
          this.loading = true
          getOaContract(data.id).then(response => {
            this.form = response.data
            this.typeOneBeforeChange = response.data.typeOne
            this.form.isPayment = response.data.isPayment
            this.form.isSeal = response.data.isSeal
            this.form.oaContractPaymentList.forEach(item => {
              if (item.investMain !== null && item.investMain !== undefined && item.investMain !== '') {
                getLpViewByMainBodyId(item.investMain).then(response => {
                  if (response.data !== null) {
                    var temp = { dictValue: response.data.id, dictLabel: response.data.name }
                    this.oaContractPlanInvestMainOptions.push(temp)
                    this.oaContractPlanInvestMainOptionsTemp.push(temp)
                  }
                })
              }
            })
            this.form.oaContractPlanList.forEach(item => {
              const tempOldData = {
                id: item.id,
                investId: item.investId,
                collectionTime: item.collectionTime,
                collectionMoney: item.collectionMoney,
                currency: item.currency,
                remark: item.remark
              }
              item.historicalData = JSON.stringify(tempOldData)
            })
            this.getTermsDictTypes()
            this.typeOneChange(this.form.typeOne, false)
            this.loading = false
          })
        } else {
          this.loading = true
          this.reset()
          if (this.form.isSeal === null || this.form.isSeal === '' || this.form.isSeal === undefined) {
            this.form.isSeal = 'N'
          }
          if (this.form.isPayment === null || this.form.isPayment === undefined || this.form.isPayment === '') {
            this.form.isPayment = 'no'
          }
          this.getTermsDictTypes()
          // 根据选择的用户不同显示部门
          getUser(this.$store.state.user.userId).then(response => {
            this.form.applyDeptId = response.data.deptId
            this.form.useDeptId = response.data.deptId
            this.loading = false
          })
        }
      },
      // 表单重置
      reset() {
        this.form = {
        id: undefined,
        applyUserId: this.$store.state.user.userId,
        applyTime: new Date(),
        signingTime: undefined,
        applyDeptId: undefined,
        useDeptId: undefined,
        name: undefined,
        money: undefined,
        typeOne: undefined,
        typeTwo: undefined,
        typeOther: undefined,
        conteractNum: undefined,
        company: undefined,
        usefulLife: undefined,
        purchasingApplication: undefined,
        usingCompanyAndTemplates: 'N',
        modifyTheTemplate: undefined,
        reasonForModification: undefined,
        specifications: undefined,
        projectOrFund: undefined,
        isPayment: undefined,
        isSeal: undefined,
        useSealOne: undefined,
        useSealTwo: undefined,
        abstracts: undefined,
        paymentPrerequisites: undefined,
        actTitle: undefined,
        createBy: undefined,
        updateBy: undefined,
        createTime: undefined,
        updateTime: undefined,
        delFlag: undefined,
          fileList: [],
          oaContractTermsList: []
        }
        this.resetForm('form')
      },
      // 取消按钮
      cancel() {
        this.$parent.$parent.formOpen = false
        this.$parent.$parent.reset()
      },
      // 关闭form页面
      closeForm() {
        this.oaContractPlanInvestMainOptions = []
        this.oaContractPlanInvestMainOptionsTemp = []
        // 关闭form页面遮罩层
        this.loading = false
        // 关闭form页面
        if (this.$parent.$parent.formOpen !== undefined) {
          this.$parent.$parent.formOpen = false
        }
        // 刷新list页面
        if (this.$parent.$parent.getList !== undefined) {
          this.$parent.$parent.getList()
        }
      },
      /** 提交按钮 */
      submitForm: function(type) {
        var flag = true
        // 用印选择 是 校验
        var seal = this.form.isSeal
        // 收付款类型 校验
        var temp = ''
        if (seal === 'Y') {
          temp = this.form.oaSealDetailList
          if (temp === null || temp === undefined || temp === '' || temp <= 0) {
            this.msgError(this.$t('至少添加一条用印明细!'))
            flag = false
          }
        }
        /*
          var payment = this.form.isPayment
          if (payment === 'collection' || payment === 'payment' || payment === 'no') {
          temp = this.form.oaContractPaymentList
          if (temp === null || temp === undefined || temp === '' || temp <= 0) {
            this.msgError(this.$t('至少添加一条签约主体!'))
            flag = false
          }
        }*/
        return new Promise((resolve, reject) => {
          this.$refs['form'].validate(valid => {
            // 验证数据
            if (valid && flag) {
              // 弹出遮罩层
              this.loading = true
              // 保存数据
              saveOaContract(this.form).then(response => {
                if (response.code === 200) {
                  // 点击的按钮是保存并启动流程
                  if (type === 'process') {
                    // 设置数据id方式启动流程后取消并再次启动会创建多条记录
                    this.form = response.data
                    // 启动流程
                    this.startProcess(response.data.id)
                    // 点击的仅保存
                  } else {
                    this.msgSuccess(this.$t('保存成功'))
                    // 关闭form页面
                    this.closeForm()
                    resolve()
                  }
                } else {
                  this.msgError(response.msg)
                }
              }).catch(() => {
                this.loading = false
                reject()
              })
            } else {
              this.locationError(document)
            }
          })
        })
      },
      // 启动流程
      startProcess: function(dataId) {
        this.loading = false
        // 启动流程时候传入的参数
        const processData = {}
        // 待办标题
        processData.title = this.form.actTitle
        // 业务表名
        processData.dataTable = 'oa_contract'
        // 业务ID
        processData.dataId = dataId
        // 业务表单地址
        processData.formPath = '@/views/oa/contract/oaContractForm'

        // 业务表单打印地址
        processData.viewPath = '@/views/oa/contract/oaContractView'

        // 菜单标识(根据菜单标识判断的走哪个流程)
        processData.menuKey = this.menuKey
        this.startProcessByMenu(processData, () => {
          this.form.actStatus = 'D'
          this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
          this.closeForm()
        })
      },
        // 用印明细
        addPayment() {
          const row = {
            id: undefined,
            investMainType: '',
            investMain: '',
            sealType: '',
            remark: ''
          }
          if (!(this.form.oaSealDetailList instanceof Array)) {
            this.$set(this.form, 'oaSealDetailList', [])
          }
          this.$nextTick(() => {
            this.form.oaSealDetailList.push(row)
          })
        },
        // 删除用印明细
        delHandle(index, row) {
          // eslint-disable-next-line no-undef
          const id = row.id
          if (id !== undefined && id !== '' && id != null) {
            this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
              confirmButtonText: this.$t('确定'),
              cancelButtonText: this.$t('取消'),
              type: 'warning'
            })
              .then(function() {
                return deleteOaSealDetail(id)
              }).then(() => {
              this.form.oaSealDetailList.splice(index, 1)
              return this.msgSuccess(this.$t('删除成功'))
            })
              .catch(function() {})
          } else {
            this.form.oaSealDetailList.splice(index, 1)
          }
        },
        // 付款信息
        addPaymentPay() {
          const row = {
            id: undefined,
            investType: '',
            investMain: '',
            investMainName: '',
            name: '',
            type: '',
            currency: 'CNY',
            remark: '',
            remindTime: '',
            remindUserId: ''
          }
          if (!(this.form.oaContractPaymentList instanceof Array)) {
            this.$set(this.form, 'oaContractPaymentList', [])
          }
          this.$nextTick(() => {
            this.form.oaContractPaymentList.push(row)
          })
        },
        // 添加收付款计划
        addPaymentPlan() {
          const row = {
            id: undefined,
            name: '',
            type: '',
            currency: 'CNY',
            remark: '',
            remindTime: '',
            remindUserId: ''
          }
          if (!(this.form.oaContractPlanList instanceof Array)) {
            this.$set(this.form, 'oaContractPlanList', [])
          }
          this.$nextTick(() => {
            this.form.oaContractPlanList.push(row)
          })
        },
        // 删除付款信息
        delHandlePay(index, row) {
          // eslint-disable-next-line no-undef
          const id = row.id
          if (id !== undefined && id !== '' && id != null) {
            this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
              confirmButtonText: this.$t('确定'),
              cancelButtonText: this.$t('取消'),
              type: 'warning'
            }).then(() => {
              this.delTempPlanList(index)
              this.changeInvestData()
              this.form.oaContractPaymentList.splice(index, 1)
              this.msgSuccess(this.$t('删除成功'))
              return delOaContractPayment(id)
            }).catch(function() {
            })
          } else {
            this.delTempPlanList(index)
            this.changeInvestData()
            this.form.oaContractPaymentList.splice(index, 1)
          }
        },
        delTempPlanList(index) {
          var tempDelId = ''
          for (var i = 0; i < this.oaContractPlanInvestMainOptionsTemp.length; i++) {
            if (this.oaContractPlanInvestMainOptionsTemp[i].dictValue === this.form.oaContractPaymentList[index].investMain) {
              tempDelId = this.oaContractPlanInvestMainOptionsTemp[i].dictValue
              this.oaContractPlanInvestMainOptionsTemp.splice(i, 1)
            }
          }

          if (this.form.oaContractPlanList !== undefined) {
            for (var j = 0; j < this.form.oaContractPlanList.length; j++) {
              if (this.form.oaContractPlanList[j].investId === tempDelId) {
                this.form.oaContractPlanList.splice(j, 1)
              }
            }
          }
        },
        // 收付款计划删除
        delHandlePlan(index, row, type) {
          const id = row.id
          if (id !== undefined && id !== '' && id != null) {
            this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
              confirmButtonText: this.$t('确定'),
              cancelButtonText: this.$t('取消'),
              type: 'warning'
            })
              .then(function() {
                return delOaContractPlan(id)
            }).then(() => {
              this.form.oaContractPlanList.splice(index, 1)
              this.sumMoneyPayColl(type)
              return this.msgSuccess(this.$t('删除成功'))
            })
              .catch(function() {})
          } else {
            this.form.oaContractPlanList.splice(index, 1)
          }
        },
        // 添加关键条款
        addPaymentTerms() {
          const row = {
            id: undefined,
            investType: '',
            investMain: '',
            paymentTime: '',
            paymentMoney: '',
            collectionTime: '',
            collectionMoney: '',
            currency: '',
            stake: undefined,
            remark: '',
            status: 1,
            reminderFrequency: 'TIMER',
            cron: '',
            frequencyCronValue: undefined
          }
          if (!(this.form.oaContractTermsList instanceof Array)) {
            this.$set(this.form, 'oaContractTermsList', [])
          }
          this.$nextTick(() => {
            this.form.oaContractTermsList.push(row)
          })
        },
        // 删除关键条款
        delHandleTerms(index, row) {
          // eslint-disable-next-line no-undef
          const id = row.id
          if (id !== undefined && id !== '' && id != null) {
            this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
              confirmButtonText: this.$t('确定'),
              cancelButtonText: this.$t('取消'),
              type: 'warning'
            })
              .then(function() {
                return delOaContractTerms(id)
              }).then(() => {
              this.form.oaContractTermsList.splice(index, 1)
              return this.msgSuccess(this.$t('删除成功'))
            })
              .catch(function() {})
          } else {
            this.form.oaContractTermsList.splice(index, 1)
          }
        },
      typeOneChange(typeOne, clearTwo) {
        if (clearTwo) {
          this.form.typeTwo = undefined
        }
        this.getTypeOneChange = []
        const object = this.typeOneOptions.find(obj => obj.dictValue === typeOne)
        var typeOneChildren
        if (object) {
          typeOneChildren = object.childDict
        }
        if (typeOneChildren !== undefined && typeOneChildren !== null && typeOneChildren !== '') {
          this.getDicts(typeOneChildren).then(response => {
            this.getTypeOneChange = response.data
          })
        }
      },
      // 添加附件
      uploader() {
        this.uploaderOpen = true
        this.title = '上传附件'
      },
      // 获取附件列表
      getData(fileList) {
        this.form.fileList = fileList
      },
      // 删除附件
      delAdjunct(index, file) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          type: 'warning'
        })
          .then(function() {
            return deleteFileBase(file.id)
          }).then(() => {
          this.form.fileList.splice(index, 1)
          return this.msgSuccess(this.$t('删除成功'))
          })
          .catch(function() {})
      },
      // 选择项目获取对应的投资主体
      schange(type) {
        if (type !== null && type !== undefined && type !== '') {
          if (this.form.oaSealDetailList === undefined) {
            var querySD = { dealId: type }
            this.oaContractPaymentLoading = true
            listDealInvestment(querySD).then(response => {
              if (response.rows.length > 0) {
                var oaSealDetailList = []
                for (var i = 0; i < response.rows.length; i++) {
                  var temp = {}
                  temp.investMainType = response.rows[i].investorType
                  temp.investMain = response.rows[i].investorId
                  oaSealDetailList.push(temp)
                }
                this.form.oaSealDetailList = oaSealDetailList
              }
              this.oaContractPaymentLoading = false
            })
          }
          if (this.form.oaContractPaymentList === undefined) {
            var queryPaymen = { dealId: type }
            this.oaContractPaymentLoading = true
            listDealInvestment(queryPaymen).then(response => {
              if (response.rows.length > 0) {
                var oaContractPaymentList = []
                for (var i = 0; i < response.rows.length; i++) {
                  var temp = {}
                  temp.investType = response.rows[i].investorType
                  temp.investMain = response.rows[i].investorId
                  oaContractPaymentList.push(temp)
                }
                this.form.oaContractPaymentList = oaContractPaymentList
                this.form.oaContractPlanList = response.data.oaContractPlanList
              }
              this.oaContractPaymentLoading = false
            })
          }
        }
      },
      // 自动计算预计付款金额 或 预计收款金额
      sumMoneyPayColl(type) {
        if (this.form.oaContractPlanList !== null && this.form.oaContractPlanList !== undefined) {
          var temp = 0
          // 付款
          if (type === 'payment') {
            this.form.oaContractPlanList.forEach(item => {
              item.collectionMoney = null
              item.collectionTime = null
              temp += this.formatNum(item.paymentMoney === undefined ? 0 : item.paymentMoney)
            })
          }
          // 收款
          if (type === 'collection') {
            this.form.oaContractPlanList.forEach(item => {
              item.paymentMoney = null
              item.paymentTime = null
              temp += this.formatNum(item.collectionMoney === undefined ? 0 : item.collectionMoney)
            })
          }
          if (isNaN(temp)) {
            temp = this.form.money
          }
          this.form.money = temp
          this.requiredVerification()
        }
      },
      /** 格式化金额, 保留精度及小数, 4位小数 **/
      formatNum(num) {
        if (num === null || num === undefined || isNaN(num)) {
          num = 0
        }
        return ruoyiFloat().formatNum(Number(num).toFixed(4))
      },
      // 刷新必填
      requiredVerification() {
        if (this.form.isPayment !== 'collection') {
          this.oaContractPlanListRules.collectionMoney = []
          this.oaContractPlanListRules.collectionTime = []
          this.oaContractPlanListRules.paymentMoney = [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
          this.oaContractPlanListRules.paymentTime = [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
        } else {
          this.oaContractPlanListRules.paymentMoney = []
          this.oaContractPlanListRules.paymentTime = []
          this.oaContractPlanListRules.collectionMoney = [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
          this.oaContractPlanListRules.collectionTime = [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
        }
      },
      changeIsPay(type) {
        this.sumMoneyPayColl(type)
      },
      changeInvestData(svalue, name) {
        var flagAddPlan = true
        var tempCount = 0
        // 签约主体中是否存在重复
        var paymentListId = []
        var tempPay = this.form.oaContractPaymentList
        for (var i = 0; i < tempPay.length; i++) {
          if (tempPay[i].investMain === svalue) {
            tempCount++
            if (tempCount > 1) {
              this.msgError(this.$t('签约主体存在重复'))
              const row = {
                investMainType: tempPay.investType,
                investMain: ''
              }
              if (!(this.form.oaContractPaymentList instanceof Array)) {
                this.$set(this.form, 'oaContractPaymentList', '')
              }
              this.$nextTick(() => {
                this.form.oaContractPaymentList.push(row)
              })
              // 删掉
              this.form.oaContractPaymentList.splice(i, 1)
              flagAddPlan = false
            }
          }
          paymentListId.push(tempPay[i].investMain)
        }
        // 收款计划 添加下拉元素
        if (flagAddPlan) {
          if (name !== undefined && svalue !== undefined) {
            var temp = {
              dictLabel: name,
              dictValue: svalue
            }
            this.oaContractPlanInvestMainOptionsTemp.push(temp)
          }
        }
        // 整理最终结果
        this.oaContractPlanInvestMainOptions = []
        this.oaContractPlanInvestMainOptionsTemp = this.toRepeatOption(this.oaContractPlanInvestMainOptionsTemp)
        var planOptionsArr = this.oaContractPlanInvestMainOptionsTemp
        tempPay.forEach(item => {
          planOptionsArr.forEach(itemPlan => {
            if (itemPlan.dictValue === item.investMain) {
              this.oaContractPlanInvestMainOptions.push(itemPlan)
            }
          })
        })
        this.oaContractPlanInvestMainOptions = this.toRepeatOption(this.oaContractPlanInvestMainOptions)
        // 当签约主体的主体名称发生变化时, 收付款计划主体会变成id. 这里就是把他设置成空字符
        var planArrTemp = this.form.oaContractPlanList
        if (planArrTemp !== undefined && planArrTemp !== null) {
          planArrTemp.forEach((item) => {
            if (!paymentListId.includes(item.investId) && item.investId !== '' && item.investId !== undefined && item.investId !== null) {
              item.investId = undefined
            }
          })
        }
      },
      // 数组去重复
      toRepeatOption(arr) {
        var hash = {}
        return arr.reduce(function(item, next) {
          hash[next.dictValue] ? '' : hash[next.dictValue] = true && item.push(next)
          return item
        }, [])
      },
      checkStake(val, index) {
        if (val !== null && val !== '' && val !== undefined) {
          if (val > 100) {
            this.form.oaContractPaymentList[index].stake = undefined
          }
        }
      },
      // 合同类型是项目默认带出当前项目的投资主体
      getInvestDate: function(id) {
        // 根据Id查出这个项目是那种类型的项目.
        this.getTermsDictTypes()
        this.form.oaContractPaymentList = []
        if (!this.StrNotBlank(this.form.id)) {
          if (this.form.typeOne === 'deal' && !this.StrNotBlank(this.form.oaSealDetailList)) {
            this.contractInvestLoading = true
            findByDealId(id).then(response => {
              if (response.data !== null) {
                var data = response.data
                data.forEach(item => {
                  var investTemp = {
                    investMain: item.investorId,
                    investType: item.investorType
                  }
                  this.form.oaContractPaymentList.push(investTemp)
                })
              }
              this.form.oaContractPaymentList.forEach(item => {
                if (item.investMain !== null && item.investMain !== undefined && item.investMain !== '') {
                  this.oaContractPlanInvestMainOptions = []
                  getLpViewByMainBodyId(item.investMain).then(response => {
                    if (response.data !== null) {
                      var temp = { dictValue: response.data.id, dictLabel: response.data.name }
                      this.oaContractPlanInvestMainOptions.push(temp)
                      this.oaContractPlanInvestMainOptionsTemp.push(temp)
                    }
                  })
                }
              })
              this.contractInvestLoading = false
            })
          }
        }
      },
      getTermsDictTypes() {
        if (this.form.typeOne === 'deal' && this.form.projectOrFund) {
          getLpView(this.form.projectOrFund).then(response => {
            if (response.code === 200) {
              if (response.data && response.data[0]) {
                if (response.data[0].dealType === 'bf') {
                  this.getDictDealType('bf')
                } else {
                  this.getDictDealType('')
                }
              }
            }
          })
        } else {
          this.getDictDealType('')
        }
      },
      // 不同位置展示不同条款类型
      getDictDealType(dealType) {
        // 股权, 债权 条款类型
        if (dealType === 'bf') {
          this.getDicts('key_terms_type_bf').then(response => {
            this.typeOptions = response.data
            this.sortTermsTypeValue()
          })
        } else {
          // 参股基金条款类型
          this.getDicts('key_terms_tyoe').then(response => {
            this.typeOptions = response.data
            this.sortTermsTypeValue()
          })
        }
      },
      // 清空条款里的 乱码值 整理条款类型值
      sortTermsTypeValue() {
        if (this.form.oaContractTermsList) {
          var oaTerms = []
          this.typeOptions.some(item => {
            oaTerms.push(item.dictValue)
          })
          this.form.oaContractTermsList.forEach(item => {
            if (!oaTerms.includes(item.type)) {
                item.type = undefined
            }
          })
        }
      },
      StrNotBlank(val) {
        if (val !== null && val !== '' && val !== undefined) {
          return true
        } else {
          return false
        }
      },
      changeIsPayment(val) {
        if (val === 'TZXY' || val === 'BCXY') {
          this.form.isPayment = 'payment'
        } else {
          this.form.isPayment = 'no'
        }
        if (val !== 'other') {
          this.form.typeOther = undefined
        }
      },
      remindConfirmHandle() {
        this.$refs['remindForm'].validate(valid => {
          if (valid) {
            this.remindOpen = false
          }
        })
      },
      remindHandle(index) {
        this.termsIndex = index
        this.remindOpen = true
      }

    }
  }
</script>
